#include"configure.h"
Configure::Configure(QWidget *parent):QDialog(parent)
{
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
    ui.setupUi(this);
    confdata = GetDataInfo();
    ui.savepathtext->setText(confdata.savepath);
    ui.pointcountext->setText(QString::number(confdata.pointcount));
    connect(ui.okbutton,SIGNAL(clicked()),this,SLOT(SaveData()));
    connect(ui.cancelbutton,SIGNAL(clicked()),this,SLOT(close()));
    connect(ui.browerbutton,SIGNAL(clicked()),this,SLOT(BrowserDir()));
}

Configure::~Configure(void)
{

}

void Configure::SaveData(void)
{
    db = QSqlDatabase::addDatabase("QSQLITE","SaveData");
    db.setDatabaseName("./database");
    db.open();
    QSqlQuery query = db.exec("update configure set savepath ='"+ui.savepathtext->text()+"',"+"pointcount = '"+ui.pointcountext->text() +"'");
    db.commit();
    db.close();
    QSqlDatabase::removeDatabase("SaveData");
    this->close();
    confdata.pointcount = ui.pointcountext->text().toInt();
    confdata.savepath = ui.pointcountext->text();
    emit ConfigChange(confdata);
}

void Configure::BrowserDir(void)
{
    QFileDialog dialog;
    dialog.setFileMode(QFileDialog::Directory);
    QStringList fileNames;
    if (dialog.exec())
    {
        fileNames = dialog.selectedFiles();
        ui.savepathtext->setText(fileNames.at(0));
    }
}
